//https://www.nowcoder.com/questionTerminal/d2efe600e73d47a2ba1533dc926cbb46
//https://leetcode.cn/problems/diagonal-traverse/description/


#include <iostream>
#include <vector>
#include <climits>
#include <stack>
#include <queue>

using namespace std;

#include <iostream>
#include <vector>
 
using namespace std;
 
void PrintLine(vector<vector<int>> &matrix, int tr, int tc, int dr, int dc, bool flag){
    if (flag){
        while(tr != dr+1){
            cout << matrix[tr++][tc--] << " ";
        }
    }
    else{
        while ( dr != tr-1){
            cout << matrix[dr--][dc++] << " ";
        }
    }
}
 
int main(){
    int n, m;
    cin >> n >> m;
    vector<vector<int>> matrix(n, vector<int>(m, 0));
    for (int i=0; i<n; i++){
        for (int j=0; j<m; j++){
            cin >> matrix[i][j];
        }
    }
    int tr = 0;
    int tc = 0;
    int dr = 0;
    int dc = 0;
    bool flag = false;
    while (tr != n){
        PrintLine(matrix, tr, tc, dr, dc, flag);
        tr = tc == m-1 ? tr + 1 : tr;
        tc = tc == m-1 ? tc : tc + 1;
        dc = dr == n-1 ? dc + 1 : dc;
        dr = dr == n-1 ? dr : dr + 1;
        flag = !flag;
    }
    return 0;
}


class Solution{
public:
    void Matrix(int m,int n){
        vector<vector<int>> vec(m,vector<int>(n,0));
        int cur_left_row=0;
        int cur_left_col=0;
        int cur_right_row=0;
        int cur_right_col=0;

        while(cur_left_col<n){
            //std::cout << "left_row:"<<cur_left_row << ",left_col:"<<cur_left_col<<std::endl;
            //std::cout << "right_row:"<<cur_right_row << ",right_col:"<<cur_right_col<<std::endl;
            if(cur_left_row<m-1){
                cur_left_row=cur_left_row+1;
                cur_left_col=cur_left_col;
            }else {
                cur_left_row=cur_left_row;
                cur_left_col=cur_left_col+1;
            }
            if(cur_right_col<n-1){
                cur_right_col=cur_right_col+1;
                cur_right_row=cur_right_row;
            }else {
                cur_right_col=cur_right_col;
                cur_right_row=cur_right_row+1;
            }
        }
    }

};